﻿using MaterialDesignThemes.Wpf;
using Prism.Commands;
using StarryEdge.Core.Parameter;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace StarryEdge.Menu.User.ViewModels.Dialog
{
    public class LoginPopupDialog : Prism.Mvvm.BindableBase
    {
        private string _name;
        /// <summary>
        /// 账号
        /// </summary>
        public string Name
        {
            get { return _name; }
            set => SetProperty(ref _name, value);
        }
        private string _loginMessage;
        /// <summary>
        /// 登录消息
        /// </summary>
        public string LoginMessage
        {
            get { return _loginMessage; }
            set => SetProperty(ref _loginMessage, value);
        }

        private Brush _textBrush;
        /// <summary>
        /// 文本颜色
        /// </summary>
        public Brush TextBrush
        {
            get { return _textBrush; }
            set => SetProperty(ref _textBrush, value);
        }
        public ObservableCollection<string> NameList { get; set; }
        private IUserEntity _login;
        public LoginPopupDialog(IUserEntity login)
        {
            _login = login;
            NameList = new ObservableCollection<string>(login.Users.Select(x => x.Name));
        }

        public DelegateCommand<System.Windows.Controls.PasswordBox> Login => new DelegateCommand<System.Windows.Controls.PasswordBox>(password =>
        {
            TextBrush = Brushes.Black;
            LoginMessage = $"登陆中，请等待...";
            if (_login.Login(Name, password.Password))
            {
                if (DialogHost.IsDialogOpen("Global"))
                {
                    DialogHost.Close("Global");
                }
            }
            else
            {
                LoginMessage = $"登录失败，账号或密码错误";
                TextBrush = Brushes.Red;
                password.Password = null;
            }
        });
    }
}
